﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DatabaseManager;
using Physio10Context;

namespace Physio_Clinic
{
    public partial class _Default : System.Web.UI.Page
    {
        public Qf8soUser currentUser = null;  
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Session["SelectedDate"] != null)
                {
                    DateCalendar.SelectedDate = (DateTime)Session["SelectedDate"];
                    DayPilotCalendar1.StartDate = (DateTime)Session["SelectedDate"];
                }
                else
                {
                    DateCalendar.SelectedDate = DateTime.Today;
                    DayPilotCalendar1.StartDate = DateTime.Today;
                }
            }

            DayPilotCalendar1.ShowHeader = true;

            if (DateCalendar.SelectedDate.DayOfWeek == DayOfWeek.Sunday || DateCalendar.SelectedDate.DayOfWeek == DayOfWeek.Saturday)
            {
                DayPilotCalendar1.BusinessBeginsHour = 10;
                DayPilotCalendar1.BusinessEndsHour = 21;
            }
            else
            {
                DayPilotCalendar1.BusinessBeginsHour = 18;
                DayPilotCalendar1.BusinessEndsHour = 21;
            }

            if (Session["nDays"] != null)
            {
                DayPilotCalendar1.Days = (int)Session["nDays"];

                if (DayPilotCalendar1.Days == 1)
                    RadioButton4.Checked = true;
                else
                    RadioButton5.Checked = true;
            }
            

            if (Request["userId"] != null || Request.QueryString["userId"] != null || Session["userId"] != null)
            {
                int userId;
                if (Request.QueryString["userId"] != null)
                {
                    Session["userId"] = Request.QueryString["userId"];
                }

                if (Request["userId"] != null)
                {
                    Session["userId"] = Request["userId"];
                }

                if (Session["userId"] != null)
                    userId =  Convert.ToInt32(Session["userId"]);
                else
                    userId = Convert.ToInt32(Request.QueryString["userId"]);


                currentUser = PersistentManager.Instance.GetUser(userId);
                Session.Add("userId", userId);
                Session["currentUser"] = currentUser;
                Session["currentUserId"] = currentUser.Id;

                if (currentUser != null)
                    userText.Text = currentUser.Username;

                PersistentManager.Instance.GenerateBookingTime(DateTime.Today);

                DoctorToday.Text = PersistentManager.Instance.DoctorWorking(DateCalendar.SelectedDate.DayOfWeek);

               // Session["SelectedDate"] = DateCalendar.SelectedDate;
            }

           
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (TimeReserve.SelectedValue == "")
                return;

           int timeValue =  Convert.ToInt32(TimeReserve.SelectedValue);

           Bookingtime bookingTime = PersistentManager.Instance.GetBookingTime(timeValue);
           bookingTime.Available = false;

           Booking new_booking = new Booking();

           new_booking.AppointmentTime = bookingTime.StartTime;
           new_booking.EndappointmentTime = bookingTime.EndTime;
           new_booking.UserId = currentUser.Id;
           new_booking.UserName = currentUser.Name;


           PersistentManager.Instance.Add(new_booking);
          // PersistentManager.Instance.Add(doctor_event);
           PersistentManager.Instance.Save();

           LinqDataSource.DataBind();
           LinqDataSource1.DataBind();

           Session["SelectedDate"] = DateCalendar.SelectedDate;

           Response.Redirect("Reserve.aspx?userId=" + currentUser.Id);
           
        }

        protected void Button1_Click1(object sender, EventArgs e)
        {
        }

        protected void DateCalendar_SelectionChanged(object sender, EventArgs e)
        {
            PersistentManager.Instance.GenerateBookingTime(DateCalendar.SelectedDate.Date);

            DayPilotCalendar1.StartDate = DateCalendar.SelectedDate;
            DoctorToday.Text = PersistentManager.Instance.DoctorWorking(DateCalendar.SelectedDate.DayOfWeek);

            if (DateCalendar.SelectedDate.DayOfWeek == DayOfWeek.Sunday || DateCalendar.SelectedDate.DayOfWeek == DayOfWeek.Saturday)
            {
                DayPilotCalendar1.BusinessBeginsHour = 10;
                DayPilotCalendar1.BusinessEndsHour = 21;
            }
            else
            {
                DayPilotCalendar1.BusinessBeginsHour = 18;
                DayPilotCalendar1.BusinessEndsHour = 21;
            }
            LinqDataSource1.DataBind();


            
        }

        protected void DayPilotCalendar1_EventClick(object sender, DayPilot.Web.Ui.EventClickEventArgs e)
        {
            Drugorder order = PersistentManager.Instance.GetDrugOrderbyBook(Convert.ToInt32(e.Value));

            if(order != null)
                Session["orderId"] = order.OrderId;

            Response.Redirect("Invoice.aspx?bookId=" + e.Value);
        }

        protected void RadioButton4_CheckedChanged(object sender, EventArgs e)
        {
            if (RadioButton4.Checked == true)
            {
                DayPilotCalendar1.Days = 1;
                Session["nDays"] = 1;
                
            }

            Response.Redirect("Reserve.aspx");
        }

        protected void RadioButton6_CheckedChanged(object sender, EventArgs e)
        {

        }

        protected void RadioButton5_CheckedChanged(object sender, EventArgs e)
        {
            if (RadioButton5.Checked == true)
            {
                DayPilotCalendar1.Days = 7;
                Session["nDays"] = 7;
                Response.Redirect("Reserve.aspx");
            }

            Response.Redirect("Reserve.aspx");
        }
    }
}
